package com.avengers.mobile.layer.rest;



import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Repository;



import com.avengers.mobile.layer.model.Producto;
import com.avengers.mobile.layer.service.ProductoService;
import com.google.gson.Gson;

@Repository("restProducto")
@Path("/products")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ProductoRest {

	static final Log log = LogFactory.getLog(ProductoRest.class);


	@Autowired
	ProductoService productService;
		
	Gson json = new Gson();	

	@GET	
	@Produces( { MediaType.APPLICATION_JSON })
	public Response  findProductoList()throws Exception { 	
		return Response.ok(json.toJson(productService.findProductoList())).build();		
	}		
	
	@Path("/{id}")
	@GET
	public Response find(@PathParam("id") int id) throws Exception{
		Producto producto = productService.findProductoById(id);		
		if (producto == null) {
			return Response.status(Status.INTERNAL_SERVER_ERROR).entity("El producto no existe").build();
		}
		return Response.ok(json.toJson(producto)).build();		
	}
	
}



